//
//  LCCycleImageView.m
//  UI_06_封装轮播图
//
//  Created by li cheng on 16/7/21.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "LCCycleImageView.h"

@interface LCCycleImageView()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *cycleScrollView;
@property (nonatomic, strong) UIPageControl *imagesPage;
@end

@implementation LCCycleImageView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        // 初始化中属性赋值得使用下划线_,不能使用self
        // 子控件初始化时子控件不要和初始化的frame一样,应该用bounds/CgRectMake(0, 0, width, height);
        [self initScrollView:frame];
        [self initImagePage:frame];
    }
    return self;
}

- (void)initScrollView:(CGRect)frame{
    _cycleScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    _cycleScrollView.backgroundColor = [UIColor blackColor];
    _cycleScrollView.pagingEnabled = YES;
    _cycleScrollView.bounces = YES;
    _cycleScrollView.delegate = self;
    _cycleScrollView.showsHorizontalScrollIndicator = NO;
    [self addSubview:_cycleScrollView];

}

//#error
#warning 后期需要添加page的位置改变,这里的默认位置
- (void)initImagePage:(CGRect)frame{
    _imagesPage = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, frame.size.width / 3, 20)];
    _imagesPage.numberOfPages = 5;
    _imagesPage.center = CGPointMake(frame.size.width / 2, frame.size.height - 20 / 2);
    [_imagesPage addTarget:self action:@selector(imagePageChange:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_imagesPage];

}

- (void)imagePageChange:(UIPageControl *)pageControl{
    [_cycleScrollView setContentOffset:CGPointMake((pageControl.currentPage + 1) * self.bounds.size.width, 0) animated:NO];
}

// 自定义方法
- (void)setImagesWithArray:(nonnull NSArray *)array{
    // 判断类型(不知道别人使用你的类的时候传进来的是什么类型)
    id firstObject = [array firstObject];
    // 最终图片数组
    NSMutableArray *imageArray = [NSMutableArray array];
    // 判断对象是不是某种类型, isKindOfClass
    if ([firstObject isKindOfClass:[NSString class]]) {
        // 把 字符串 转成 UIImage 数组
        for (NSString *imageName in array) {
            UIImage *image = [UIImage imageNamed:imageName];
            [imageArray addObject:image];
        }
        // 使用数组创建ImageView
    } else if ([firstObject isKindOfClass:[UIImage class]]) {
        [imageArray setArray:array];
    }
    // 获取到第一张图
    UIImage *firstImage = [imageArray firstObject];
    // 获取到最后一张图
    UIImage *lastImage = [imageArray lastObject];
    // 下标为0的位置放最后一张图
    [imageArray insertObject:lastImage atIndex:0];
    // 在最后添加第一张图
    [imageArray addObject:firstImage];
    NSLog(@"%ld", imageArray.count);
    self.imageArray = [NSArray arrayWithArray:imageArray];
    // 使用数组创建ImageView
    for (int i = 0; i <imageArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.bounds.size.width, 0, self.bounds.size.width, self.bounds.size.height)];
        imageView.image =imageArray[i];
        [_cycleScrollView addSubview:imageView];
    }
    _cycleScrollView.contentSize = CGSizeMake(imageArray.count * self.bounds.size.width, 0);
    // 因为添加了最后一张图,要让轮播图看起来像第一张图
    _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
    _imagesPage.numberOfPages = imageArray.count - 2;
    
}

- (void)createAllImageViewsWithImagesArray:(NSArray *)array{
    
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger page = scrollView.contentOffset.x / self.bounds.size.width;
    _imagesPage.currentPage = page - 1;
    //实现轮播
    if (page == 0) {
        _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width * (_imageArray.count - 2), 0);
        _imagesPage.currentPage = _imageArray.count - 1 - 2;
    }else if (page == _imageArray.count - 1){
        _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
        _imagesPage.currentPage = 0;
        
    }
}






@end
